package com.easylinkin.linkappapi.grid.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.easylinkin.linkappapi.common.model.BaseController;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.grid.entity.GridReportWeekDetail;
import com.easylinkin.linkappapi.grid.models.dto.GridReportWeekDetailSaveDTO;
import com.easylinkin.linkappapi.grid.models.vo.GridReportWeekDetailCollectVO;
import com.easylinkin.linkappapi.grid.service.IGridReportWeekDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Setter;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import java.util.Arrays;


/**
 * 网格周 报填写详情 前端控制器
 *
 * @author kuyujie
 * @since 2024-05-22
 */
@Api(tags = "网格周报填写详情")
@Setter(onMethod_ = @Resource)
@RestController
@RequestMapping("/grid/gridReportWeekDetail")
public class GridReportWeekDetailController extends BaseController {

    private IGridReportWeekDetailService gridReportWeekDetailService;

    /**
     * 分页列表查询
     *
     * @param requestModel
     * @return
     */
    @ApiOperation(value = "分页列表查询", notes = "分页列表查询")
    @PostMapping(value = "/listPage")
    public RestMessage queryPageList(@RequestBody RequestModel<GridReportWeekDetail> requestModel) {
        // Assert.notNull(requestModel,"参数不能为空");
        // Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        if (CollUtil.isNotEmpty(requestModel.getSorts())) {
            requestModel.getSorts().forEach(s -> {
                requestModel.getPage().getOrders().add(new OrderItem().setColumn(StrUtil.toUnderlineCase(s.getField())).setAsc("ASC".equalsIgnoreCase(s.getSortRule())));
            });
        } else {
            requestModel.getPage().getOrders().add(new OrderItem().setColumn("id").setAsc(false));
        }
        IPage<GridReportWeekDetail> pageList = gridReportWeekDetailService.page(requestModel.getPage(), Wrappers.lambdaQuery(requestModel.getCustomQueryParams()));
        return RestBuilders.successBuilder().data(pageList).build();
    }

    /**
     * 保存
     *
     * @param dto
     * @return
     */
    @ApiOperation(value = "保存", notes = "保存")
    @PostMapping(value = "/save")
    public RestMessage save(@RequestBody GridReportWeekDetailSaveDTO dto) {
        this.setOperator(dto);
        gridReportWeekDetailService.saveData(dto);
        return RestBuilders.successBuilder().message("保存成功").build();
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @DeleteMapping(value = "/delete")
    public RestMessage delete(@RequestParam("id") Integer id) {
        gridReportWeekDetailService.removeById(id);
        return RestBuilders.successBuilder().message("删除成功").build();
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除", notes = "批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public RestMessage deleteBatch(@RequestParam("ids") String ids) {
        this.gridReportWeekDetailService.removeByIds(Arrays.asList(ids.split(",")));
        return RestBuilders.successBuilder().message("批量删除成功").build();
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping(value = "/queryById")
    public RestMessage queryById(@RequestParam("id") Integer id) {
        GridReportWeekDetail gridReportWeekDetail = gridReportWeekDetailService.getById(id);
        if (gridReportWeekDetail == null) {
            return RestBuilders.errorBuilder().message("未找到对应数据").build();
        }
        return RestBuilders.successBuilder().data(gridReportWeekDetail).build();
    }

    /**
     * 获取周报汇总数据
     */
    @GetMapping(value = "/getDetailCollect")
    public RestMessage getDetailCollect(@RequestParam("reportId") Long reportId) {
        GridReportWeekDetailCollectVO data = gridReportWeekDetailService.getDetailCollect(reportId);
        return RestBuilders.successBuilder().data(data).build();
    }
}
